using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using BL;
using Telerik.WinControls.UI;
using BO;

namespace AnimalFarm.Product_manage
{
    public partial class ListBulkProductField : Telerik.WinControls.UI.RadForm
    {
        B_BulkProduct_Field B_bulkProductField = new B_BulkProduct_Field();
        B_Field B_field = new B_Field();
        B_BulkProduct B_bulkProduct = new B_BulkProduct();
        int bulkProductID, fieldID;
        public ListBulkProductField()
        {
            InitializeComponent();
        }

        //load bulk product field
        private void loadBulkProductField()
        {
            DataTable aTable = B_bulkProductField.GetAllBulkProduct_Field();
            grvBulkProductField.DataSource = aTable;
        }
        private void ListBulkProductField_Load(object sender, EventArgs e)
        {
            loadBulkProductField();
            loadContextMenu();
        }

        //load context menu
        private void loadContextMenu()
        {
            ContextMenu menu = new ContextMenu();
            MenuItem item = new MenuItem();
            item.Text = "Delete";
            menu.MenuItems.Add(item);
            grvBulkProductField.ContextMenu = menu;
            item.Click += new EventHandler(item_Click);
        }

        void item_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show(this,"Are you sure","Deleting product",MessageBoxButtons.YesNo,MessageBoxIcon.Warning);
            if (result == DialogResult.Yes)
            {
                int bulkProductID = int.Parse(grvBulkProductField.SelectedRows[0].Cells["BulkProductID"].Value.ToString());
                int fieldID = int.Parse(grvBulkProductField.SelectedRows[0].Cells["FieldID"].Value.ToString());
                bool isOk = B_bulkProductField.RemoveOneBulkProduct_Field(fieldID, bulkProductID);
                if (isOk)
                {
                    MessageBox.Show(this, "Product deleted successful", "Product deleting", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    loadBulkProductField();
                }
                else
                {
                    MessageBox.Show(this, "Can not delete this product", "Product deleting", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void grvBulkProductField_Click(object sender, EventArgs e)
        {
            try
            {
                if (grvBulkProductField.SelectedRows.Count > 0)
                {
                    GridViewRowInfo row = grvBulkProductField.SelectedRows[0];
                    bulkProductID = int.Parse(row.Cells["BulkProductID"].Value.ToString());
                    fieldID = int.Parse(row.Cells["FieldID"].Value.ToString());
                    int quanlity = int.Parse(row.Cells["Quantity"].Value.ToString());

                    DateTime time = DateTime.Parse(row.Cells["HarvestTme"].Value.ToString());
                    string productName = row.Cells["ProductName"].Value.ToString();

                    Field_Info fieldInfo = B_field.GetOneField(fieldID);

                    txtFieldName.Text = fieldInfo.FieldName;
                    txtProductName.Text = productName;
                    txtQuanlity.Text = quanlity + "";
                    dtpTime.Value = time;
                }
            }
            catch (Exception ex) { }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void btnSaveChange_Click(object sender, EventArgs e)
        {
            try
            {
                BulkProduct_Field_Info info = new BulkProduct_Field_Info();
                info.BulkProductID = bulkProductID;
                info.FieldID = fieldID;
                info.HarvestTme = dtpTime.Value;
                info.ProductName = txtProductName.Text;
                try
                {
                    info.Quantity = int.Parse(txtQuanlity.Text);
                }
                catch (Exception ex) { info.Quantity = 0; }
                bool isOk = B_bulkProductField.EditOneBulkProduct_Field(info);
                if (isOk)
                {
                    MessageBox.Show(this, "Product updated successful", "Product updating", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    loadBulkProductField();
                }
                else
                {
                    MessageBox.Show(this, "Product updating is fail", "Product updating", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex) { }
        }
    }
}
